package com.loong.android;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Surface;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class LonWin extends AppCompatActivity {

    private Map<String, WinLife> mWinLifeMap;
    private LonWin instance;

    /**
     * 添加窗口生命周期事件监听器
     *
     * @param name     名称
     * @param listener 监听器
     */
    public void addWinLifeListener(String name, WinLife listener) {
        if (TextUtils.isEmpty(name) || listener == null) return;
        if (mWinLifeMap == null) mWinLifeMap = new HashMap<>();
        mWinLifeMap.put(name, listener);
    }

    /**
     * 删除窗口生命周期事件监听器
     *
     * @param name 名称
     */
    public void removeWinLifeListener(String name) {
        if (mWinLifeMap == null || TextUtils.isEmpty(name)) return;
        mWinLifeMap.remove(name);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onCreate(savedInstanceState);
            }
        }
        onWinCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onResume();
            }
        }
        onWinReady();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onStart();
            }
        }
        onWinStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onRestart();
            }
        }
        onWinRestart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onPause();
            }
        }
        onWinPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onStop();
            }
        }
        onWinStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onDestroy();
            }
        }
        onWinDestroy();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onWindowFocusChanged(hasFocus);
            }
        }
        onWinFocusChanged(hasFocus);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onNewIntent(intent);
            }
        }
        onNewWin(intent);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onConfigurationChanged(newConfig);
            }
        }
        onWinConfigChange(newConfig);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode, event);
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onKeyDown(keyCode, event);
            }
        }
        return onWinKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        super.onKeyUp(keyCode, event);
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onKeyUp(keyCode, event);
            }
        }
        return onWinKeyUp(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        super.onKeyLongPress(keyCode, event);
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onKeyLongPress(keyCode, event);
            }
        }
        return onWinKeyLongPress(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onActivityResult(requestCode, resultCode, data);
            }
        }
        onOtherWinClose(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (mWinLifeMap != null) {
            for (Map.Entry<String, WinLife> entry : mWinLifeMap.entrySet()) {
                WinLife winLife = entry.getValue();
                if (winLife == null) continue;
                winLife.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
        onWinPermissionsResult(requestCode, permissions, grantResults);
    }

    /**
     * 窗口被创建
     *
     * @param data 传入的数据
     */
    protected void onWinCreate(Bundle data) {
    }

    /**
     * 窗口就绪完毕
     */
    protected void onWinReady() {
    }

    /**
     * 窗口启动完毕
     */
    protected void onWinStart() {
    }

    /**
     * 窗口被重启
     */
    protected void onWinRestart() {
    }

    /**
     * 窗口被暂停
     */
    protected void onWinPause() {
    }

    /**
     * 窗口被停止
     */
    protected void onWinStop() {
    }

    /**
     * 窗口被销毁
     */
    protected void onWinDestroy() {
    }

    /**
     * 窗口焦点改变
     *
     * @param hasFocus 是否获得焦点
     */
    protected void onWinFocusChanged(boolean hasFocus) {
    }

    /**
     * 窗口创建新实例
     *
     * @param intentData 传入数据
     */
    protected void onNewWin(Intent intentData) {
    }

    /**
     * 窗口配置改变
     *
     * @param newConfig 新配置数据
     */
    protected void onWinConfigChange(Configuration newConfig) {
    }

    /**
     * 窗口按下某键
     * 
     * @param keyCode 键代码
     * @param event 事件对象
     */
    protected boolean onWinKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    /**
     * 窗口弹起某键
     *
     * @param keyCode 键代码
     * @param event 事件对象
     */
    protected boolean onWinKeyUp(int keyCode, KeyEvent event) {
        return false;
    }

    /**
     * 窗口长按某键
     *
     * @param keyCode 键代码
     */
    protected boolean onWinKeyLongPress(int keyCode, KeyEvent event) {
        return false;
    }

    /**
     * 当其他窗口被关闭
     *
     * @param requestCode 请求码
     * @param resultCode  结果码
     * @param intent      传入数据
     */
    protected void onOtherWinClose(int requestCode, int resultCode, Intent intent) {
    }

    /**
     * 权限请求结果回调
     *
     * @param requestCode  请求码
     * @param permissions  请求的权限数组
     * @param grantResults 授权结果数组，授权结果：0=允许；-1=禁止
     */
    protected void onWinPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    }

    /**
     * 取上下文
     */
    public Context getContext() {
        return instance;
    }

    /**
     * 加载布局
     *
     * @param layoutResId 布局文件资源索引
     */
    public void loadLayout(int layoutResId) {
        setContentView(layoutResId);
    }

    /**
     * 加载布局
     *
     * @param view 布局视图对象
     */
    public void loadLayout(View view) {
        setContentView(view);
    }

    /**
     * 载入窗口布局中的视图
     * @param viewId 视图id
     */
    public <T> T loadView(int viewId) {
        return (T) findViewById(viewId);
    }

    /**
     * 从指定视图对象中载入其子视图
     * @param view 视图对象
     * @param viewId 视图id
     */
    public <T> T loadView(View view, int viewId) {
        return (T) view.findViewById(viewId);
    }

    /**
     * 设置窗口背景颜色
     * @param color 颜色值
     */
    public void setWinBackgroundColor(int color) {
        GradientDrawable gd = new GradientDrawable();
        gd.setColor(color);
        getWindow().setBackgroundDrawable(gd);
    }

    /**
     * 返回桌面
     */
    public void backToDesktop() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }

    /**
     * 隐藏标题栏
     */
    public void hideTitleBar() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去除AppCompatActivity的标题栏
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
    }

    /**
     * 置窗口标题
     * @param title 标题内容
     */
    public void setWinTitle(String title) {
        setTitle(title);
    }

    /**
     * 取窗口标题
     */
    public String getWinTitle() {
        return (String) getTitle();
    }

    /**
     * 取屏幕宽度，单位px
     */
    public int getScreenWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) return 0;
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * 取屏幕高度，单位px
     */
    public int getScreenHeight() {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) return 0;
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }

    /**
     * 取屏幕宽度和高度, 返回整数型数组，[宽度, 高度]，单位px
     */
    public int[] getScreenWidthAndHeight() {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) return new int[]{0, 0};
        wm.getDefaultDisplay().getMetrics(dm);
        return new int[]{dm.widthPixels, dm.heightPixels};
    }

    /**
     * 置横屏
     */
    public void setLandscape() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    /**
     * 置竖屏
     */
    public void setPortrait() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    /**
     * 判断是否为横屏
     */
    public boolean isLandscape() {
        return getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    }

    /**
     * 判断是否为竖屏
     */
    public boolean isPortrait() {
        return getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }

    /**
     * 获取屏幕旋转角度，0、90、180或270
     */
    public int getScreenRotation() {
        switch (getWindowManager().getDefaultDisplay().getRotation()) {
            default:
            case Surface.ROTATION_0:
                return 0;
            case Surface.ROTATION_90:
                return 90;
            case Surface.ROTATION_180:
                return 180;
            case Surface.ROTATION_270:
                return 270;
        }
    }

    /**
     * 获取状态栏高度
     */
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 判断状态栏是否存在
     */
    public boolean isStatusBarExists() {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        return (params.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != WindowManager.LayoutParams.FLAG_FULLSCREEN;
    }

    /**
     * 判断虚拟按键栏是否重写
     */
    private static String getNavBarOverride() {
        String sNavBarOverride = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);
                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Throwable e) {

            }
        }
        return sNavBarOverride;
    }

    /**
     * 检查是否存在虚拟按键栏
     */
    public boolean hasNavigationBar() {
        Resources res = getResources();
        int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");
        if (resourceId != 0) {
            boolean hasNav = res.getBoolean(resourceId);
            // check override flag
            String sNavBarOverride = getNavBarOverride();
            if ("1".equals(sNavBarOverride)) {
                hasNav = false;
            } else if ("0".equals(sNavBarOverride)) {
                hasNav = true;
            }
            return hasNav;
        } else { // fallback
            return !ViewConfiguration.get(this).hasPermanentMenuKey();
        }
    }

    /**
     * 取虚拟按键栏高度
     */
    public int getNavigationBarHeight() {
        int result = 0;
        if (hasNavigationBar()) {
            Resources res = getResources();
            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = res.getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

    /**
     * 设置全屏显示，只能在loadLayout函数之前使用
     */
    public void setFullScreen() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();

        //如果已经处于全屏状态，则不需重复设置。
        if ((lp.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
                == WindowManager.LayoutParams.FLAG_FULLSCREEN) return;

        lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    /**
     * 退出全屏
     */
    public void exitFullScreen() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();

        //如果已经处于非全屏状态，则不需重复设置。
        if ((lp.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
                != WindowManager.LayoutParams.FLAG_FULLSCREEN) return;

        lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setAttributes(lp);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    /**
     * 判断是否处于全屏
     */
    public boolean isFullScreen() {
        int flag = getWindow().getAttributes().flags;
        return (flag & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN;
    }


    /**
     * 获取当前屏幕截图，包含状态栏
     */
    private Bitmap captureWithStatusBar() {
        View view = getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int width = getScreenWidth();
        int height = getScreenHeight();
        Bitmap ret = Bitmap.createBitmap(bmp, 0, 0, width, height);
        view.destroyDrawingCache();
        return ret;
    }

    /**
     * 获取当前屏幕截图，不包含状态栏
     */
    private Bitmap captureWithoutStatusBar() {
        View view = getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int statusBarHeight = getStatusBarHeight();
        int width = getScreenWidth();
        int height = getScreenHeight();
        Bitmap ret = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
        view.destroyDrawingCache();
        return ret;
    }

    /**
     * 窗口截屏
     * @param hasStatusBar 是否包含状态栏
     */
    public Bitmap screenshot(boolean hasStatusBar) {
        if (hasStatusBar) {
            return captureWithStatusBar();
        } else {
            return captureWithoutStatusBar();
        }
    }

    /**
     * 窗口截屏
     */
    public Bitmap screenshot() {
        return captureWithStatusBar();
    }

    /**
     * 判断是否锁屏
     */
    public boolean isScreenLock() {
        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        return km != null && km.inKeyguardRestrictedInputMode();
    }


    /**
     * 设置安全窗口，禁用系统截屏。防止 App 中的一些界面被截屏，并显示在其他设备中造成信息泄漏。
     * （常见手机设备系统截屏操作方式为：同时按下电源键和音量键。）
     */
    public void noScreenshots() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
    }

    /**
     * 保持屏幕常亮。仅在应用内有效, 应用退出后常亮状态恢复系统默认。
     */
    public void keepScreenOn() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    /**
     * 取消屏幕常亮。
     */
    public void cancelKeepScreenOn() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    /**
     * 设置当前窗口显示时的亮度。不影响系统亮度。
     * @param screenBrightness 亮度值。若小于0表示使用跟随系统设置的亮度，0到1表示从暗到亮。
     */
    public void setScreenBrightness(float screenBrightness) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.screenBrightness = screenBrightness;
        window.setAttributes(lp);
    }

    /**
     * 取当前窗口的亮度。返回小于0表示使用跟随系统设置的亮度，0到1表示从暗到亮。
     */
    public float getScreenBrightness() {
        return getWindow().getAttributes().screenBrightness;
    }

    /**
     * 启动窗口
     * @param targetWin 目标窗口数据
     * @param intent 意图数据
     * @param flags 标志位数组
     */
    public void startWin(Class<? extends Activity> targetWin, Intent intent, int[] flags) {
        Intent in = intent != null ? intent : new Intent();
        if (flags != null && flags.length > 0) {
            for (int flag : flags) in.addFlags(flag);
        }
        in.setClass(this, targetWin);
        startActivity(in);
    }

    /**
     * 启动窗口
     * @param intent 意图数据对象
     */
    public void startWin(Intent intent) {
        startActivity(intent);
    }

    /**
     * 启动窗口
     * @param targetWin 目标窗口数据
     */
    public void startWin(Class<? extends Activity> targetWin) {
        startWin(targetWin, null, null);
    }

    /**
     * 启动窗口
     * @param targetWin 目标窗口数据
     * @param intent 意图数据
     */
    public void startWin(Class<? extends Activity> targetWin, Intent intent) {
        startWin(targetWin, intent, null);
    }

    /**
     * 启动窗口
     * @param targetWin 目标窗口数据
     * @param flags 标志位数组
     */
    public void startWin(Class<? extends Activity> targetWin, int[] flags) {
        startWin(targetWin, null, flags);
    }

    /**
     * 启动窗口并监听这个窗口返回的结果。其他窗口返回的结果在onOtherWinClose事件中获得
     * @param targetWin 目标窗口数据
     * @param intent 意图数据
     * @param requestCode 请求码
     * @param bundle 附加数据
     * @param flags 标志位数组
     */
    public void startWinResult(Class<? extends Activity> targetWin, Intent intent, int requestCode, Bundle bundle, int[] flags) {
        Intent in = intent != null ? intent : new Intent();
        if (flags != null && flags.length > 0) {
            for (int flag : flags) {
                in.addFlags(flag);
            }
        }
        in.setClass(this, targetWin);
        startActivityForResult(in, requestCode, bundle);
    }

    /**
     * 启动窗口并监听这个窗口返回的结果。其他窗口返回的结果在onOtherWinClose事件中获得
     * @param targetWin 目标窗口数据
     * @param intent 意图数据
     * @param requestCode 请求码
     * @param flags 标志位数组
     */
    public void startWinResult(Class<? extends Activity> targetWin, Intent intent, int requestCode, int[] flags) {
        startWinResult(targetWin, intent, requestCode, null, flags);
    }

    /**
     * 启动窗口并监听这个窗口返回的结果。其他窗口返回的结果在onOtherWinClose事件中获得
     * @param targetWin 目标窗口数据
     * @param intent 意图数据
     * @param requestCode 请求码
     */
    public void startWinResult(Class<? extends Activity> targetWin, Intent intent, int requestCode) {
        startWinResult(targetWin, intent, requestCode, null, null);
    }

    /**
     * 启动窗口并监听这个窗口返回的结果。其他窗口返回的结果在onOtherWinClose事件中获得
     * @param targetWin 目标窗口数据
     * @param requestCode 请求码
     */
    public void startWinResult(Class<? extends Activity> targetWin, int requestCode) {
        startWinResult(targetWin, null, requestCode, null, null);
    }

    /**
     * 启动窗口并监听这个窗口返回的结果。其他窗口返回的结果在onOtherWinClose事件中获得
     * @param intent 意图数据
     * @param requestCode 请求码
     */
    public void startWinResult(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode);
    }

    /**
     * 启动窗口并监听这个窗口返回的结果。其他窗口返回的结果在onOtherWinClose事件中获得
     * @param targetWin 目标窗口数据
     * @param requestCode 请求码
     * @param flags 标志位数组
     */
    public void startWinResult(Class<? extends Activity> targetWin, int requestCode, int[] flags) {
        startWinResult(targetWin, null, requestCode, null, flags);
    }

    /**
     * 置关闭窗口时返回的结果码
     *
     * @param resultCode 结果码
     */
    public void setCloseWinResult(int resultCode) {
        setResult(resultCode);
    }

    /**
     * 置关闭窗口时返回的结果码和数据
     *
     * @param resultCode 结果码
     * @param intent     意图数据
     */
    public void setCloseWinResult(int resultCode, Intent intent) {
        setResult(resultCode, intent);
    }

    /**
     * 关闭所有窗口
     */
    public void closeAllWin() {
        LonApp.finishAllActivity();
    }

    /**
     * 在主线程中运行代码，一般用于多线程中将更新ui的操作提交到主线程中，
     * 因为多线程中更新ui是不安全的，不被允许的，只能通过提交到主进程进行更新。
     * @param event 回调事件，在此回调事件中添加需要在主线程中执行的代码
     */
    public static void runOnUiThread(Context context, Runnable event) {
        if(!(context instanceof Activity)) return;
        Activity activity = (Activity) context;
        activity.runOnUiThread(event);
    }

    /**
     * 判断窗口是否已销毁。android4.2(api17)及以上版本才有效，低于android4.2的版本都会返回false
     */
    public boolean isWinDestroyed() {
        if(Build.VERSION.SDK_INT < 17) return false;
        return super.isDestroyed();
    }

    /**
     * 判断窗口是否为多窗口模式。android7.0(api24)及以上版本才有效，低于android7.0的版本都会返回false
     */
    public boolean isWinInMultiWinMode() {
        if(Build.VERSION.SDK_INT < 24) return false;
        return super.isInMultiWindowMode();
    }

    /**
     * 判断窗口是否为画中画模式。android7.0(api24)及以上版本才有效，低于android7.0的版本都会返回false
     */
    public boolean isWinInPictureInPictureMode() {
        if(Build.VERSION.SDK_INT < 24) return false;
        return super.isInPictureInPictureMode();
    }

    /**
     * 判断窗口是否处于过渡动画运行过程中。android8.0(api26)及以上版本才有效，低于android8.0的版本都会返回false
     */
    public boolean isWinTransitionRunning() {
        if(Build.VERSION.SDK_INT < 26) return false;
        return super.isActivityTransitionRunning();
    }

    /**
     * 绑定服务
     *
     * @param intent 意图数据对象
     * @param connection 服务连接器对象
     */
    public void bindService(Intent intent, ServiceConnection connection) {
        super.bindService(intent, connection, BIND_AUTO_CREATE);
    }

    /**
     * 注册广播对象，仅在安卓8.0(api26)及以上版本有效。
     *
     * @param receiver 广播对象
     * @param filter 意图过滤器对象
     * @param flags 标志位
     */
    @Override
    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) {
        if(Build.VERSION.SDK_INT < 26) return null;
        return super.registerReceiver(receiver, filter, flags);
    }
}
